<?php defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

/*
 * Copyright Copyright (C) 2007 Alain Georgette. All rights reserved.
 * Copyright Copyright (C) 2006 Frantisek Hliva. All rights reserved.
 * License http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 *
 * !JoomlaComment is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * !JoomlaComment is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

class HTML_comments {
	
    function viewComments($option, &$rows, &$lists, &$search, &$pageNav, $task='')
    {
        global $mosConfig_live_site, $mainframe;
		$maxlength = "200";
		$onlydisplay = $lists['noedit'] ? true  : false;
		$adminform	 = $lists['noform'] ? false : true;
		$checkread	 = $lists['checkread'] ? true : false;
?>
<?php 	if ($adminform) { ?><form action="index2.php" method="post" name="adminForm"><?php 	} ?>

    <table cellpadding="4" cellspacing="0" border="0" width="100%" class="adminheading">
    	<tr>
      		<th width="100%" align="left"><?php echo $lists['title'] ?></th>
      		<td nowrap="nowrap">Display #</td>
      		<td> <?php echo $pageNav->writeLimitBox(); ?>
      		</td>
      		<td>Search:</td>
      		<td>
        		<input type="text" name="search" value="<?php echo $search; ?>" class="inputbox" onChange="document.adminForm.submit();" />
      		</td>
    	</tr>
    </table>
    
    <table cellpadding="4" cellspacing="0" border="0" width="100%" class="adminlist">
      <tr>
        <th width="2%" class="title"><input type="checkbox" name="toggle" <?php echo $checkread ? "DISABLED" : ""; ?> value="" onclick="checkAll(<?php echo count($rows); ?>);" /></th>
        <?php $colspan = 14; ?>
        <th class="title"><div align="center">Id</div></th>
        <th class="title"><div align="center">Author</div></th>
        <th class="title"><div align="center">Userid</div></th>
        <th class="title"><div align="center">Notify</div></th>
        <th class="title"><div align="center">Url</div></th>
        <th class="title"><div align="center">Date</div></th>
		<th class="title"><div align="left">Comment</div></th>
		<th class="title"><div align="center">ContentItem</div></th>
        <th class="title"><div align="center">Published</div></th>
        <th class="title"><div align="center">IP</div></th>
        <th class="title"><div align="center">Voting Yes</div></th>
        <th class="title"><div align="center">Voting No</div></th>
        <th class="title"><div align="center">Parent Id</div></th>
      </tr>
<?php
        $k = 0;
        for ($i = 0, $n = count($rows); $i < $n; $i++) {
            $row = &$rows[$i];
            if ($row->name == '') $row->name = 'Anonymous';
            $row->comment = stripslashes($row->comment);
            if (strlen($row->comment) > $maxlength) {
                $row->comment = substr($row->comment, 0, $maxlength);
                $row->comment .= "...";
            }
            $Itemid = $mainframe->getItemid( $row->contentid, 0, 0 );
        	if (!$Itemid) $Itemid = 99999999;
			$content = "$mosConfig_live_site/index.php?option=com_content&task=view&Itemid=$Itemid&id=$row->contentid#josc$row->id";
?>           
     <tr class="row<?php echo $k; ?>">
     	<td width='5%'>
     		<input type='checkbox'  <?php echo $checkread ? "DISABLED" : ""; ?> id='cb<?php echo $i; ?>' name='cid[]' value='<?php echo $row->id; ?>' onclick='isChecked(this.checked);' />
     	</td>
        <td align='center'>
        	<?php echo $row->id; ?>
        </td>
        <td align='center'>
<?php		if (!$onlydisplay) { 
?>
			<a href="index2.php?option=<?php echo $option; ?>&task=edit&id=<?php echo $row->id ?>">
        		<?php echo $row->name; ?>
        	</a>
<?php
			} else
				echo $row->name;
?>
        </td>
        <td align='center'>
        	<?php echo $row->userid; ?>
        </td>
        <td align='center'>
        	<a href='mailto:<?php echo $row->userid ? $row->usermail : $row->email; ?>'>
<?php
            if ($row->notify) {
                $notifyimg = "mailgreen.jpg";
                $notifytxt = "notify if new post";
                $notifyalt = "yes";
            } else {
                $notifyimg = "mailred.jpg";
                $notifytxt = "not notify if new post";
                $notifyalt = "no";
            } 
?>
				<img border=0 src="./../components/com_comment/joscomment/images/<?php echo $notifyimg; ?>" title="<?php echo $notifytxt; ?>" alt="<?php echo $notifyalt; ?>">
			</a>
		</td>                
        <td align='center' width="5%">
        	<?php echo $row->website ? "<a href='$row->website'>url</a>" : ""; ?>
        </td>
        <td align='center'>
        	<?php echo $row->date; ?>
        </td>
        <td align='left' width="10%">
        	<b><?php echo $row->title; ?></b>
        	<br /><?php echo $row->comment; ?>
        </td>
        <td width='15%' align='center'>
        	<a href="<?php echo $content; ?>" target="_blank"><?php echo $row->ctitle; ?></a>
        </td>
<?php
            $published = $row->published ? 'unpublish' : 'publish';
            $img = $row->published ? 'publish_g.png' : 'publish_x.png';
?>
        <td width="10%" align="center">
<?php		if (!$onlydisplay) { 
?>               
        	<a href="javascript:return void(0);" onclick="return listItemTask('cb<?php echo $i; ?>','<?php echo $published; ?>')">
<?php
			}
?>        	
        		<img src="images/<?php echo $img; ?>" width="12" height="12" border="0" alt="" />
<?php		if (!$onlydisplay) { 
?>               
        	</a>
<?php
			}
?>
        </td>
        <td align='center'>
        	<?php echo $row->ip; ?>
        </td>
        <td align='center'>
        	<?php echo $row->voting_yes; ?>
        </td>
        <td align='center'>
        	<?php echo $row->voting_no; ?>
        </td>
        <td align='center'>
        	<?php echo $row->parentid; ?>
        </td>
     </tr>
<?php 
			$k = 1 - $k;
        }
?>
    <tr>
      <th align="center" colspan="<?php echo $colspan; ?>">
        <?php echo $pageNav->writePagesLinks(); ?>
      </th>
    </tr>
    <tr>
      <td align="center" colspan="<?php echo $colspan; ?>">
        <?php echo $pageNav->writePagesCounter(); ?>
      </td>
    </tr>
  </table>
  <input type="hidden" name="boxchecked" value="0" />
<?php 	if ($adminform) { ?>
  <input type="hidden" name="option" value="<?php echo $option; ?>" />
  <input type="hidden" name="task" value="<?php echo $task; ?>" />
  </form>
<?php 	} ?>

<?php
    }

    function editComment($option, &$row, &$lists)
    {
        mosMakeHtmlSafe($row, ENT_QUOTES, 'comment');

?>
    <script language="javascript" type="text/javascript">
    function submitbutton(pressbutton) {
      var form = document.adminForm;
      if (pressbutton == 'cancel') {
        submitform( pressbutton );
        return;
      }

      if (form.comment.value == ""){
        alert( "You must at least write the comment text." );
      } else if (form.contentid.value == "0"){
        alert( "You must select a corresponding content item." );
      } else {
        submitform( pressbutton );
      }
    }
    </script>
    <table cellpadding="4" cellspacing="0" border="0" width="100%">
    <tr>
      <td width="100%"><span class="sectionname"><?php echo $row->id ? 'Edit' : 'Add'; ?> Comment</span></td>
    </tr>
  </table>
    <table cellpadding="4" cellspacing="1" border="0" width="100%" class="adminform">
    <form action="index2.php" method="post" name="adminForm" id="adminForm">
      <tr>
        <td width="20%" align="right">Name:</td>
        <td width="80%">
          <input class="inputbox" type="text" name="name" size="50" maxlength="30" value="<?php echo $row->name;

        ?>" />
        </td>
      </tr>
      <tr>
        <td width="20%" align="right">Userid:</td>
        <td width="80%"><?php echo $lists['userid']; ?> </td>
      </tr>
      <tr>
        <td width="20%" align="right">Email:</td>
        <td width="80%">
          <input class="inputbox" type="text" name="email" size="50" maxlength="50" value="<?php echo $row->email;

        ?>" />
        </td>
      </tr>

      <tr>
        <td width="20%" align="right">Website:</td>
        <td width="80%">
          <input class="inputbox" type="text" name="website" size="50"  maxlength="100" value="<?php echo $row->website;

        ?>" />
        </td>
      </tr>

      <tr>
        <td width="20%" align="right">Notify:</td>
        <td width="80%">
          <?php echo mosHTML::yesnoRadioList('notify', 'class="inputbox"', $row->notify); ?>
        </td>
      </tr>

      <tr>
        <td valign="top" align="right">Content Item:</td>
        <td>
          <?php echo $lists['content'];

        ?>
        </td>
      </tr>

      <tr>
        <td valign="top" align="right">Title:</td>
        <td>
          <input class="inputbox" type="text" name="title" value="<?php echo $row->title;

        ?>" size="50" maxlength="50" />
        </td>
      </tr>

      <tr>
        <td valign="top" align="right">Comment:</td>
        <td>
          <textarea class="inputbox" cols="50" rows="5" name="comment"><?php echo $row->comment;

        ?></textarea>
        </td>
      </tr>

      <tr>
        <td valign="top" align="right">Published:</td>
        <td>
          <?php echo $lists['published'];

        ?>
        </td>
      </tr>

    </table>

    <input type="hidden" name="id" value="<?php echo $row->id;

        ?>" />
    <input type="hidden" name="option" value="<?php echo $option;

        ?>" />
    <input type="hidden" name="task" value="" />
    </form>
<?php
    }

	function importPanel($option, $task, $fromtable='', $fromcomponent='') {
	    global $database;
	    
	    /* check comment is empty */
	    $database->setQuery("SELECT id FROM #__comment LIMIT 1");
    	if ($database->loadResult())
        	mosRedirect("index2.php?option=$option", 'The joomlacomment table is not empty. This tool is for initialization of comments FROM AN OTHER COMMENT COMPONENT.');
        
	    echo "<form action='index2.php' method='POST' name='adminForm'>";
	    
	    $tabs = new mosTabs(1);
        $tabs->startPane("jos_importpanel");
        
        $tabs->startTab("Mapping", "mapping");
        importMapping($option, $task, $fromtable, $fromcomponent);
        $tabs->endTab();

        $tabs->startTab("Preview", "preview");
        previewImportComments($option, $task, $fromtable);
        $tabs->endTab();
                
        $tabs->endPane();
        
        echo hidden('task',$task);  /* set value for display and search in preview */
        echo hidden('option', $option);
        
        echo "</form>";        
	}
	
	function importMapping( &$lists ) {
	           
        $rows = new tabRows();

        echo hidden('component', '1'); /* to initialise and after allows to change */
                
		$rows->createRow('title'	, 'Import Comments Mapping');
		$rows->createRow('parameter', 
			'Import from standard component', $lists['fromcomponent'], 
			'<b>If your component is available in the list, select its value will propose automatically below the table and columns according to this component</b>.'
			. ' There should be missing columns dependent of the release of the components, verify the mapping and if needed, you can change it manually !'
			. ' It is possible that some columns have no equivalent in the source system. In this case leave them empty. For example mXcomment is using rating, not voting yes, and voting no. We affect "voting_yes" joomlacomment to "rating" and we leave empty "voting_no".'
			. '<br /><b>If your component is not in the list, you have to do the mapping manually</b>, selecting first the table, then the columns (see below).'
			. '<br />If you need help, do not hesitate to contact the joomlacomment support !'
			);
		$rows->createRow('separator');
		$rows->createRow('parameter', 
		'Import from table <b style="color:red;">(obligatory)</b>', $lists['fromtable'], 'Select the <b>database table which contains the comments to import</b>' );
		$rows->createRow('parameter', 
		'--- Id from the column <b style="color:red;">(obligatory)</b>', mosHTML::selectList( $lists['columns'], 'id', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['id'] ), 'Select the column which contains the <b>Comment Id</b>.' );
		$rows->createRow('parameter', 
		'--- Content Id from the column <b style="color:red;">(obligatory)</b>', mosHTML::selectList( $lists['columns'], 'contentid', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['contentid'] ), 'Select the column which contains the <b>Content Item Id</b>' );
		$rows->createRow('parameter', 
		'--- Date from the column', mosHTML::selectList( $lists['columns'], 'date', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['date'] ), 'Select the column which contains the <b>Date of the comment</b>' );
		$rows->createRow('parameter', 
		'--- Name from the column', mosHTML::selectList( $lists['columns'], 'name', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['name'] ), 'Select the column which contains the <b>Name</b> of the comment writer' );
		$rows->createRow('parameter', 
		'--- Userid from the column', mosHTML::selectList( $lists['columns'], 'userid', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['userid'] ), 'Select the column which contains the <b>Userid</b> of the comment writer' );
		$rows->createRow('parameter', 
		'--- IP from the column', mosHTML::selectList( $lists['columns'], 'ip', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['ip'] ), 'Select the column which contains the <b>IP</b> of the comment writer' );
		$rows->createRow('parameter', 
		'--- Email from the column', mosHTML::selectList( $lists['columns'], 'email', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['email'] ), 'Select the column which contains the <b>Email</b> of the comment writer' );
		$rows->createRow('parameter', 
		'--- Notify parameter from the column', mosHTML::selectList( $lists['columns'], 'notify', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['notify'] ), 'Select the column which contains the <b>Notify parameter</b> of the comment writer (notify if new post paramter)' );
		$rows->createRow('parameter', 
		'--- Website from the column', mosHTML::selectList( $lists['columns'], 'website', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['website'] ), 'Select the column which contains the <b>Website</b> of the comment writer' );
		$rows->createRow('parameter', 
		'--- Title from the column', mosHTML::selectList( $lists['columns'], 'title', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['title'] ), 'Select the column which contains the <b>Title</b> of the comment' );
		$rows->createRow('parameter', 
		'--- Text from the column', mosHTML::selectList( $lists['columns'], 'comment', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['comment'] ), 'Select the column which contains the <b>Text</b> of the comment' );
		$rows->createRow('parameter', 
		'--- Voting Yes count from the column', mosHTML::selectList( $lists['columns'], 'voting_yes', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['voting_yes'] ), 'Select the column which contains the <b>Voting Yes count</b> of the comment' );
        $rows->createRow('parameter', 
		'--- Voting No count from the column', mosHTML::selectList( $lists['columns'], 'voting_no', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['voting_no'] ), 'Select the column which contains the <b>Voting No count</b> of the comment' );
        $rows->createRow('parameter', 
		'--- Published from the column', mosHTML::selectList( $lists['columns'], 'published', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['published'] ), 'Select the column which contains the <b>Published parameter</b> of the comment' );
        $rows->createRow('parameter', 
		'--- Parent id from the column', mosHTML::selectList( $lists['columns'], 'parentid', ' class="inputbox" ', 'Field', 'desc', $lists['sel_columns']['parentid'] ), 'Select the column which contains the <b>Parent Id</b> of the comment (when comment is linked as a child -- response -- of another comment)' );
		$rows->createRow('separator');
		$rows->createRow('parameter', 
		'Save sql queries after import execution ?', $lists['savequeries'], 'will save the sql queries in a file (directory: media).' );
    
        echo $rows->tabRows_htmlCode();
        
    }  
    
}

?>